package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryPlanTargetAccountReq;
import com.sg.dto.base.app.req.QueryPlanTargetAccountDetailReq;
import com.sg.dto.base.app.req.QueryPlanTargetAccountListReq;
import com.sg.entity.OmsPlanTargetAccount;
import com.sg.service.base.app.MOmsPlanTargetAccountService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsPlanTargetAccountMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsPlanTargetAccountServiceImpl
        extends ServiceImpl<OmsPlanTargetAccountMapper, OmsPlanTargetAccount>
        implements MOmsPlanTargetAccountService {

    @Autowired
    OmsPlanTargetAccountMapper omsPlanTargetAccountMapper;

    /**
     * 3-9-01查规划目标账户详情[2650]
     * gen by moon at 9/13/2022, 12:35:07 AM
     */
    @Trace(operationName = "3-9-01查规划目标账户详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsPlanTargetAccount queryPlanTargetAccountDetail(QueryPlanTargetAccountDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsPlanTargetAccount> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsPlanTargetAccount> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsPlanTargetAccount retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-9-01新增规划目标账户[2732]
     * gen by moon at 9/13/2022, 12:35:08 AM
     */
    @Trace(operationName = "3-9-01新增规划目标账户")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addPlanTargetAccount(OmsPlanTargetAccount omsPlanTargetAccount) {
        omsPlanTargetAccount.setPlanTargetAccountId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsPlanTargetAccount.setAccountNumber(CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsPlanTargetAccount.setBatchEndtTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        omsPlanTargetAccount.setIsUpdate("TRUE");
        omsPlanTargetAccount.setUpdateStatus("NEW");
        omsPlanTargetAccount.setArchivngType("NOT_ARCHIVE");
        omsPlanTargetAccount.setIsEditing("TRUE");
        omsPlanTargetAccount.setIsArchive("FALSE");
        omsPlanTargetAccount.setSubjectLifeCycle("EDITING");
        omsPlanTargetAccount.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsPlanTargetAccount.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsPlanTargetAccount.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsPlanTargetAccount.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsPlanTargetAccount.setIsValid("FALSE");
        omsPlanTargetAccount.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsPlanTargetAccount.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsPlanTargetAccount)) {
            return omsPlanTargetAccount.getPlanTargetAccountId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-9-01查规划目标账户列表[2738]
     * gen by moon at 12/7/2023, 6:14:48 PM
     */
    @Trace(operationName = "3-9-01查规划目标账户列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsPlanTargetAccount> queryPlanTargetAccountList(QueryPlanTargetAccountListReq reqDto) {

        try {
            QueryWrapper<OmsPlanTargetAccount> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsPlanTargetAccount.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsPlanTargetAccount.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsPlanTargetAccount.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsPlanTargetAccount>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-9-01批量删规划目标账户[2739]
     * gen by moon at 9/13/2022, 12:35:09 AM
     */
    @Trace(operationName = "3-9-01批量删规划目标账户")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeletePlanTargetAccount(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 3-9-01批量查规划目标账户[2807]
     * gen by moon at 9/13/2022, 12:35:10 AM
     */
    @Trace(operationName = "3-9-01批量查规划目标账户")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsPlanTargetAccount> batchQueryPlanTargetAccount(BatchQueryPlanTargetAccountReq reqDto) {

        try {
            QueryWrapper<OmsPlanTargetAccount> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            if (reqDto.getPlanTargetAccountList() == null || reqDto.getPlanTargetAccountList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("entity_id", reqDto.getPlanTargetAccountList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsPlanTargetAccount.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsPlanTargetAccount.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsPlanTargetAccount.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsPlanTargetAccount>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-9-01批量改规划目标账户[3630]
     * gen by moon at 9/13/2022, 12:35:10 AM
     */
    @Trace(operationName = "3-9-01批量改规划目标账户")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdatePlanTargetAccount(List<OmsPlanTargetAccount> omsPlanTargetAccount) {

        if (omsPlanTargetAccount == null || omsPlanTargetAccount.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsPlanTargetAccount);

    }

}

